package com.kgc.kmall.managerservice.service;

import com.kgc.kmall.bean.*;
import com.kgc.kmall.managerservice.mapper.*;
import com.kgc.kmall.service.SpuService;
import org.apache.dubbo.config.annotation.Service;

import javax.annotation.Resource;
import java.util.List;
@Service
public class SpuServiceImpl implements SpuService {
    @Resource
    PmsProductInfoMapper pmsProductInfoMapper;
    @Resource
    PmsBaseSaleAttrMapper pmsBaseSaleAttrMapper;
    @Resource
    PmsProductImageMapper pmsProductImageMapper;

    @Resource
    PmsProductSaleAttrMapper pmsProductSaleAttrMapper;
    @Resource
    pmsProductSaleAttrValueMapper pmsProductSaleAttrValueMappers;
    @Override
    public List<PmsProductInfo> seleSpuList(Long catalog3Id) {
        PmsProductInfoExample example=new PmsProductInfoExample();
        PmsProductInfoExample.Criteria criteria = example.createCriteria();
        criteria.andCatalog3IdEqualTo(catalog3Id);
        return pmsProductInfoMapper.selectByExample(example);
    }

    @Override
    public List<PmsBaseSaleAttr> baseSaleAttrList() {
        return pmsBaseSaleAttrMapper.selectByExample(null);
    }

    @Override
    public Integer addPmsProductInfo(PmsProductInfo pmsProductInfo) {
        int insert = pmsProductInfoMapper.insert(pmsProductInfo);
        List<PmsProductImage> spuImageList = pmsProductInfo.getSpuImageList();
        Long productInfoId = pmsProductInfo.getId();
        if(spuImageList!=null&&spuImageList.size()>0){
            pmsProductImageMapper.addPmsProductImages(productInfoId,spuImageList);
        }
        List<PmsProductSaleAttr> spuSaleAttrList = pmsProductInfo.getSpuSaleAttrList();
        if(spuSaleAttrList!=null&&spuSaleAttrList.size()>0){
            pmsProductSaleAttrMapper.addPmsProductSaleAttr(productInfoId,spuSaleAttrList);
            for (PmsProductSaleAttr pmsProductSaleAttr : spuSaleAttrList) {
                List<pmsProductSaleAttrValue> spuSaleAttrValueList = pmsProductSaleAttr.getSpuSaleAttrValueList();
                if(spuSaleAttrValueList!=null&&spuSaleAttrValueList.size()>0){
                    for (pmsProductSaleAttrValue pmsProductSaleAttrValue : spuSaleAttrValueList) {
                        pmsProductSaleAttrValue.setProductId(productInfoId);
                        pmsProductSaleAttrValueMappers.insert(pmsProductSaleAttrValue);
                    }
                }
            }
        }
        return insert;
    }

    @Override
    public List<PmsProductSaleAttr> spuSaleAttrList(Long spuId) {
        PmsProductSaleAttrExample example=new PmsProductSaleAttrExample();
        PmsProductSaleAttrExample.Criteria criteria = example.createCriteria();
        criteria.andProductIdEqualTo(spuId);
        List<PmsProductSaleAttr> saleAttrList = pmsProductSaleAttrMapper.selectByExample(example);
        if(saleAttrList!=null&&saleAttrList.size()>0){
            for (PmsProductSaleAttr pmsProductSaleAttr : saleAttrList) {
                    pmsProductSaleAttrValueExample example1=new pmsProductSaleAttrValueExample();
                    pmsProductSaleAttrValueExample.Criteria criteria1 = example1.createCriteria();
                    criteria1.andProductIdEqualTo(spuId);
                    criteria1.andSaleAttrIdEqualTo(pmsProductSaleAttr.getSaleAttrId());
                    List<pmsProductSaleAttrValue> pmsProductSaleAttrValues = pmsProductSaleAttrValueMappers.selectByExample(example1);
                    pmsProductSaleAttr.setSpuSaleAttrValueList(pmsProductSaleAttrValues);
            }
        }
        return saleAttrList;
    }

    @Override
    public List<PmsProductImage> seleProductImagesById(Long spuId) {
        PmsProductImageExample example=new PmsProductImageExample();
        PmsProductImageExample.Criteria criteria = example.createCriteria();
        criteria.andProductIdEqualTo(spuId);
        List<PmsProductImage> pmsProductImages = pmsProductImageMapper.selectByExample(example);
        return pmsProductImages;
    }

    @Override
    public List<PmsProductSaleAttr> spuSaleAttrListIsCheckService(Long spuId, Long skuId) {
        List<PmsProductSaleAttr> saleAttrList = pmsProductSaleAttrMapper.spuSaleAttrListIsCheck(spuId, skuId);
        return saleAttrList;
    }
}
